<?php
/**
 * Description: 获取错题收藏
 * User: devforma
 * Date: 15/4/9
 * Time: 14:54
 */
require __DIR__ . '/includes/utils.php';
class CollectErrorQuestions implements iController {
    private $userId, $availableNotes;

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        $this->userId = $queryArgs['user_id'];

        //获取该用户能看到的知识点
        $userData = getUserData($this->userId);
        if (empty($userData) || empty($userData->note_info))
            return genOutput(1000);

        //用户可见的知识点列表
        $this->availableNotes = array_keys(normalizeUserNoteInfo($userData->note_info));

        //知识点id参数统一化
        if (!empty($queryArgs['note_id']) && filter_var($queryArgs['note_id'], FILTER_VALIDATE_INT)) {
            $originalNoteId = $queryArgs['note_id'];
        } elseif (!empty($queryArgs['hierarchy_3']) && filter_var($queryArgs['hierarchy_3'], FILTER_VALIDATE_INT)) {
            $originalNoteId = $queryArgs['hierarchy_3'];
        } elseif (!empty($queryArgs['hierarchy_2']) && filter_var($queryArgs['hierarchy_2'], FILTER_VALIDATE_INT)) {
            $originalNoteId = $queryArgs['hierarchy_2'];
        } elseif (!empty($queryArgs['hierarchy_1']) && filter_var($queryArgs['hierarchy_1'], FILTER_VALIDATE_INT)) {
            $originalNoteId = $queryArgs['hierarchy_1'];
        } else {
            return genOutput(1000);
        }

        //查询所给知识点下的叶子知识点
        $noteIds = getLeafNotesFromHierarchy($originalNoteId, $this->availableNotes);
        return genOutput(1, $this->getQuestionIdsByNote($queryArgs['type'], $noteIds));
    }

    /**
     * 根据知识点id来抽错题、收藏
     * 
     * @param $type
     * @param $noteIds
     * @return array
     */
    private function getQuestionIdsByNote($type, $noteIds) {

        global $uddb;

        $table = $uddb->table($uddb->qbUserTests)->sublist($this->userId);
        if ($type == 'collect') {
            $sql = "SELECT `question_id`, `user_answer`, `is_right`, `is_collected` FROM `{$table}` WHERE `user_id`=:user_id AND
             `note_id` IN (".implode(',', $noteIds).") AND `is_collected`=1 AND `collect_time` IS NOT NULL";

        } else {
            $sql = "SELECT `question_id`, `user_answer`, `is_collected`, `is_right` FROM `{$table}` WHERE `user_id`=:user_id AND
             `note_id` IN (".implode(',', $noteIds).") AND `is_right`=0 AND `user_answer`!='' AND `test_time` IS NOT NULL";
        }
        $questions = $uddb->getResults($sql, array(':user_id' => $this->userId));

        $answers = array();
        $qids = array();
        foreach ($questions as $q) {
            if (in_array($q->question_id, $qids, true))
                continue;

            $answers[] = array(
                'id' => intval($q->question_id),
                'is_right' => !empty($q->is_right) ? true : false,
                'is_collected' => !empty($q->is_collected) ? true : false,
                'answer' => $q->user_answer
            );

            $qids[] = $q->question_id;
        }

        return array(
            'questions' => getQuestionsAndNotesByIds(['user_id' => $this->userId,'ids' => $qids]),
            'answers' => $answers
        );
    }
}